#!/usr/bin/perl -w
use strict;
use warnings;
use Net::SNMP;
use Data::Dumper;
use threads;
use threads::shared;
use FileHandle;
require "/var/www/cgi-bin/common_snmp.pl";

BEGIN { our $start_run = time(); }

# global variables
my $duration = 10;
my %Numeric_Oid_arr = ();
my @numeric_oid_arr;
my %ap_list = ();
my %client_list = ();
my @threads;
my ($client_ip, $ap_ip, $client_ap, $client_mac_dec, $client_mac_hex, $APSlotId_list, $client_APSlotId, $APs_current_channel);

# host info
my $ip = '11.37.200.5';
my $host = '10.89.234.220';
my $community = 'Altest';


# interested MIBs Infomation
my $OID_sysUpTime = '.1.3.6.1.2.1.1.3.0';
my $OID_agentCurrentCPUUtilization ='.1.3.6.1.4.1.14179.1.1.5.1.0';
my $OID_agentFreeMemory = '.1.3.6.1.4.1.14179.1.1.5.3.0';


my $OID_cisco_product = '.1.3.6.1.4.1.9.1.1279';
my $OID_bsnAPNumOfSlot = '.1.3.6.1.4.1.14179.2.2.1.1.6';
my $OID_grp_bsnMobileStationIpAddress = '.1.3.6.1.4.1.14179.2.1.4.1.2';
my $OID_grp_bsnMobileStationAPIfSlotId = '.1.3.6.1.4.1.14179.2.1.4.1.5';
my $OID_grp_bsnMobileStationAPMacAddr = '.1.3.6.1.4.1.14179.2.1.4.1.4';
my $OID_grp_bsnApIpAddress = '.1.3.6.1.4.1.14179.2.2.1.1.19';
my $OID_grp_bsnMobileStationStatsEntry = '.1.3.6.1.4.1.14179.2.1.6.1';
my $OID_grp_bsnAPIfLoadParametersEntry = '.1.3.6.1.4.1.14179.2.2.13.1';
my $OID_grp_bsnAPIfChannelInterferenceInfoEntry = '.1.3.6.1.4.1.14179.2.2.14.1';
my $OID_grp_bsnAPIfPhyChannelNumber = '.1.3.6.1.4.1.14179.2.2.2.1.4';
my $OID_grp_bsnAPIfChannelNoiseInfoEntry = '.1.3.6.1.4.1.14179.2.2.15.1';

$Numeric_Oid_arr{"sysUpTime"} = '.1.3.6.1.2.1.1.3.0';
$Numeric_Oid_arr{"agentCurrentCPUUtilization"} = '.1.3.6.1.4.1.14179.1.1.5.1.0';
$Numeric_Oid_arr{"agentFreeMemory"} = '.1.3.6.1.4.1.14179.1.1.5.3.0';

for my $key ( keys %Numeric_Oid_arr ) {
        push (@numeric_oid_arr, $Numeric_Oid_arr{$key});
}


# create SNMP session 
my ($error, $session) = snmp_connect($host, $community);

if (!$session) {
	print $error;
    exit 1;
} else {
	$client_ip = $session->get_table(-baseoid => $OID_grp_bsnMobileStationIpAddress);
	$client_ap = $session->get_table(-baseoid =>  $OID_grp_bsnMobileStationAPMacAddr);
	$ap_ip = $session->get_table(-baseoid => $OID_grp_bsnApIpAddress);
	$APSlotId_list = $session->get_table(-baseoid => $OID_grp_bsnMobileStationAPIfSlotId);
	$APs_current_channel = $session->get_table(-baseoid => $OID_grp_bsnAPIfPhyChannelNumber);
}



#print Dumper($APSlotId_list);
# collect clients info from the host (WCS)

#print "clients found: \n";
foreach my $oid (keys %$client_ip) {
#	print "1";
	$client_mac_dec = $oid;
	$client_mac_dec =~ s/$OID_grp_bsnMobileStationIpAddress\.//;
#	print $client_mac_dec . "\n";
	$client_mac_hex = format_mac_decimal($client_mac_dec);
	$client_list{$client_ip->{$oid}}{'mac_hex'} = $client_mac_hex;
	my $AP_mac_hex = format_mac_hex($client_ap->{"$OID_grp_bsnMobileStationAPMacAddr.$client_mac_dec"});
	my $AP_mac_dec = mac_hex_decimal($AP_mac_hex);
	$client_list{$client_ip->{$oid}}{'AP_hex'} = $AP_mac_hex;
	$client_list{$client_ip->{$oid}}{'AP_IP'} = $ap_ip->{"$OID_grp_bsnApIpAddress.$AP_mac_dec"};
	$client_list{$client_ip->{$oid}}{'APSlotId'} = $APSlotId_list->{"$OID_grp_bsnMobileStationAPIfSlotId.$client_mac_dec"};
	$client_list{$client_ip->{$oid}}{'channel'} = $APs_current_channel->{"$OID_grp_bsnAPIfPhyChannelNumber.$AP_mac_dec.$client_list{$client_ip->{$oid}}{'APSlotId'}"};
	
	#client and assoicated AP info logging
	my $fh = FileHandle->new();
	my $filename = $client_mac_hex. "-" . time . ".log";
	$filename =~ s/:/-/g;
	open($fh, "> $filename");
	print $fh "Client INFO\tMAC address:$client_mac_hex,IP_Address:$client_ip->{$oid},Channel:$client_list{$client_ip->{$oid}}{'channel'}\n";	
	print $fh "Assoicated AP INFO\tMAC_address:$client_list{$client_ip->{$oid}}{'AP_hex'},IP_address:$client_list{$client_ip->{$oid}}{'AP_IP'},AP_Slot_Id:$client_list{$client_ip->{$oid}}{'APSlotId'}\n";
	close $fh;
	print "$filename\n";
}
#print Dumper(\%client_list);
#my $stationstats = $session->get_request(-varbindlist => ['.1.3.6.1.4.1.14179.2.2.15.1.21.100.217.137.71.218.176.1.149'],);
#print Dumper($stationstats);

foreach my $node (keys %client_list){
	my $fh = FileHandle->new();
	my $t = threads->new(\&get_sessionstats, $session, $node, $fh, %client_list);
	push(@threads,$t);
}
#print "\n------------------------------\n";
foreach (@threads) {
        my $num = $_->join;
#        print "done with $num\n";
}

$session->close();

#print "\n" . Dumper(\%client_list);


my $end_run = time();
my $run_time = $end_run - our $start_run;
#print "\nJob took $run_time seconds\n";


sub get_sessionstats {
	my ($session, $node, $fh, %client_list) = @_;
	my $client_mac = $client_list{$node}{'mac_hex'};
	my $AP_mac_hex = $client_list{$node}{'AP_hex'};
	my $APSlotId = $client_list{$node}{'APSlotId'};
	my $filename = $client_mac. "-" . time . ".csv";
	$filename =~ s/:/-/g;
	my $client_mac_dec = mac_hex_decimal($client_mac);
	my $AP_mac_dec = mac_hex_decimal($AP_mac_hex);
	my $client_channel = $client_list{$node}{'channel'};
	my @interested_oid = ();
	my %interested_oid = (
		'bsnMobileStationStatsEntry' => {	
								'bsnMobileStationRSSI' => "$OID_grp_bsnMobileStationStatsEntry.1.client_mac_dec",
								'bsnMobileStationBytesReceived' => "$OID_grp_bsnMobileStationStatsEntry.2.client_mac_dec",
								'bsnMobileStationBytesSend' => "$OID_grp_bsnMobileStationStatsEntry.3.client_mac_dec",
								'bsnMobileStationPolicyErrors' => "$OID_grp_bsnMobileStationStatsEntry.4.client_mac_dec",
								'bsnMobileStationPacketReceived' => "$OID_grp_bsnMobileStationStatsEntry.5.client_mac_dec",
								'bsnMobileStationPacketSent' => "$OID_grp_bsnMobileStationStatsEntry.6.client_mac_dec",
								'bsnMobileStationSnr' => "$OID_grp_bsnMobileStationStatsEntry.26.client_mac_dec",
		},
		'bsnAPIfLoadParametersEntry' => {
							'bsnAPIfLoadRxUtilization' => "$OID_grp_bsnAPIfLoadParametersEntry.1.AP_mac_dec.APSlotId",
							'bsnAPIfLoadTxUtilization' => "$OID_grp_bsnAPIfLoadParametersEntry.2.AP_mac_dec.APSlotId",
							'bsnAPIfLoadChannelUtilization' => "$OID_grp_bsnAPIfLoadParametersEntry.3.AP_mac_dec.APSlotId",
							'bsnAPIfLoadNumOfClients' => "$OID_grp_bsnAPIfLoadParametersEntry.4.AP_mac_dec.APSlotId",
							'bsnAPIfLoadPoorSNRClients' => "$OID_grp_bsnAPIfLoadParametersEntry.24.AP_mac_dec.APSlotId",
		},
		'bsnAPIfLoadParametersEntry' => {
							'bsnAPIfLoadRxUtilization' => "$OID_grp_bsnAPIfLoadParametersEntry.1.AP_mac_dec.APSlotId",
							'bsnAPIfLoadTxUtilization' => "$OID_grp_bsnAPIfLoadParametersEntry.2.AP_mac_dec.APSlotId",
							'bsnAPIfLoadChannelUtilization' => "$OID_grp_bsnAPIfLoadParametersEntry.3.AP_mac_dec.APSlotId",
							'bsnAPIfLoadNumOfClients' => "$OID_grp_bsnAPIfLoadParametersEntry.4.AP_mac_dec.APSlotId",
							'bsnAPIfLoadPoorSNRClients' => "$OID_grp_bsnAPIfLoadParametersEntry.24.AP_mac_dec.APSlotId",
		},
		'bsnAPIfChannelInterferenceInfoEntry' => {
							'bsnAPIfChannelInterferencePower' => "$OID_grp_bsnAPIfChannelInterferenceInfoEntry.2.AP_mac_dec.APSlotId.client_channel",
							'bsnAPIfChannelInterferenceUtilization' => "$OID_grp_bsnAPIfChannelInterferenceInfoEntry.22.AP_mac_dec.APSlotId.client_channel",
		},
		'bsnAPIfChannelNoiseInfoEntry' => {
							'bsnAPIfChannelNoisePower' => "$OID_grp_bsnAPIfChannelNoiseInfoEntry.21.AP_mac_dec.APSlotId.client_channel",
	
		},
		'agentResourceInfoGroup' => {
							'agentCurrentCPUUtilization' => '.1.3.6.1.4.1.14179.1.1.5.1.0',
							'agentCurrentTotalMemory' => '.1.3.6.1.4.1.14179.1.1.5.2.0',
							'agentCurrentFreeMemory' => '.1.3.6.1.4.1.14179.1.1.5.3.0',
							'bsnSensorTemperature' => '.1.3.6.1.4.1.14179.2.3.1.13.0',
		},
	);
	
		
	if (! -e $filename){
		open($fh, "> $filename");
		print $fh "timestamp,";
		foreach my $key (sort keys %interested_oid){
			foreach my $oid (sort keys %{$interested_oid{$key}}){
#				print "$oid,";
				print $fh "$oid,";
				my $oid_num = $interested_oid{$key}->{$oid};
				$oid_num =~ s/AP_mac_dec/$AP_mac_dec/e; # get oid for AP mac address
				$oid_num =~ s/APSlotId/$APSlotId/e; # get oid for AP Slot Id
				$oid_num =~ s/client_mac_dec/$client_mac_dec/e; # get oid for client mac
				$oid_num =~ s/client_channel/$client_channel/e; # get oid for client channel
				push(@interested_oid, $oid_num);
			}	
		}	
		
#		print "\n";	
		print $fh "\n";	
	} else {
		open($fh, ">> $filename");
	}
	
	
	for(my $n = 0; $n<$duration; $n++){
		my $start = time();
		my $stationstats = $session->get_request(-varbindlist => \@interested_oid);
#		print "\n" . Dumper($stationstats);
		print $fh time . ",";
		foreach my $key (@interested_oid){
#			print $key . "\n";
#			print $stationstats->{"$key"} . ",";
			print $fh $stationstats->{"$key"} . ",";
		}

#		print "\n";
		print $fh "\n";
		while ((time - $start) lt 5) {}
	}
	close $fh;
}
